package com.feng.zhihao.demo.springbatch.job;

import java.util.Date;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.beans.BeansException;
import org.springframework.scheduling.quartz.QuartzJobBean;

import com.alibaba.fastjson.JSON;
import com.feng.zhihao.demo.springbatch.SpringBatchApplication;
import com.feng.zhihao.demo.springbatch.dao.BatchTaskHisMapper;
import com.feng.zhihao.demo.springbatch.dao.BatchTaskMapper;
import com.feng.zhihao.demo.springbatch.entity.BatchTask;
import com.feng.zhihao.demo.springbatch.entity.BatchTaskHis;
import com.feng.zhihao.demo.springbatch.page.Message;
import com.xiaoleilu.hutool.date.DateUtil;

public class TaskJob extends QuartzJobBean{
	Logger logger = LoggerFactory.getLogger(getClass());
	BatchTaskMapper batchTaskMapper;
	BatchTaskHisMapper batchTaskHisMapper;
	public TaskJob() {
		super();
		batchTaskMapper = SpringBatchApplication.applicationContext.getBean(BatchTaskMapper.class);
		batchTaskHisMapper = SpringBatchApplication.applicationContext.getBean(BatchTaskHisMapper.class);
	}



	@Override
	public void executeInternal(JobExecutionContext context) throws JobExecutionException {
		executeInternal(String.valueOf(context.getMergedJobDataMap().get("batchTask_id")),
				context.getFireInstanceId(),
				context.getFireTime(),
				context.getScheduledFireTime(),
				context.getNextFireTime(),
				context.getPreviousFireTime());
	}
	public void executeInternal(String batchTask_id,String fireInstanceId,Date fireTime,Date scheduledFireTime,Date nextFireTime,Date previousFireTime) {
		MDC.put("task_id", batchTask_id);
		logger.info("task id:({}) start;",batchTask_id);
		
		BatchTask batchTask = batchTaskMapper.selectByPrimaryKey(batchTask_id);
		
		BatchTask batchTask_tmp=new BatchTask();
		batchTask_tmp.setTaskId(batchTask.getTaskId());
		batchTask_tmp.setStartTime(fireTime);
		batchTask_tmp.setNextStartTime(nextFireTime);
		batchTask_tmp.setRunState(20);
		batchTaskMapper.updateByPrimaryKeySelective(batchTask_tmp);
		
		BatchTaskHis batchTaskHis = new BatchTaskHis();
		batchTaskHis.setBeanId(batchTask.getBeanId());
		batchTaskHis.setNextStartTime(nextFireTime);
		batchTaskHis.setFireTime(fireTime);
		batchTaskHis.setTaskHisId(fireInstanceId);
		batchTaskHis.setTaskId(batchTask.getTaskId());
		batchTaskHis.setPreviousTime(previousFireTime);
		batchTaskHis.setRunState(20);
		batchTaskHis.setScheduledTime(scheduledFireTime);
		batchTaskHisMapper.insertSelective(batchTaskHis);
		
		batchTask_tmp=new BatchTask();
		batchTask_tmp.setTaskId(batchTask.getTaskId());
		
		try {
			
			BusinessJob businessJob = (BusinessJob) SpringBatchApplication.applicationContext.getBean(batchTask.getBeanId());
			Message message = businessJob.execute(scheduledFireTime, batchTaskHis);
			batchTaskHis.setMessage(JSON.toJSONString(message));
			batchTask_tmp.setMessage(batchTaskHis.getMessage());
			
			batchTaskHis.setRunState(message.getCode());
			batchTask_tmp.setRunState(message.getCode());
			
		} catch (BeansException e) {
			batchTaskHis.setRunState(502);
			batchTask_tmp.setRunState(502);
			
			batchTaskHis.setMessage(e.getMessage());
			batchTask_tmp.setMessage(e.getMessage());
		} catch (Exception e) {
			batchTaskHis.setRunState(501);
			batchTask_tmp.setRunState(501);			

			batchTaskHis.setMessage(e.getMessage());
			batchTask_tmp.setMessage(e.getMessage());
		} catch (Throwable e) {
			batchTaskHis.setRunState(500);
			batchTask_tmp.setRunState(500);

			batchTaskHis.setMessage(e.getMessage());
			batchTask_tmp.setMessage(e.getMessage());			
		} 
		batchTaskHis.setExecuteTime(System.currentTimeMillis() - fireTime.getTime());
		batchTask_tmp.setExecuteTime(batchTaskHis.getExecuteTime());
		batchTaskHisMapper.updateByPrimaryKeySelective(batchTaskHis);
		batchTaskMapper.updateByPrimaryKeySelective(batchTask_tmp);
		
		logger.info("task id:({}) end; fireInstanceId:({}); bean id:({}); execute time:({}); next fire time:({})"
				,batchTask.getTaskId()
				,fireInstanceId
				,batchTask.getBeanId()
				,batchTask_tmp.getExecuteTime(),
				DateUtil.format(nextFireTime, "yyyy-MM-dd HH:mm:ss.SSS"));
	}
	

}
